package com.zjsx.lib.utils;

import android.os.SystemClock;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class TimeUtils {
    public static String getEasyTime(long time) {
        if (time <= 0) {
            return null;
        }

        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTimeInMillis(System.currentTimeMillis());

        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTimeInMillis(time);
        
        String timeStr = null;
        if (calendar1.get(Calendar.YEAR) != calendar2.get(Calendar.YEAR)
                || calendar1.get(Calendar.MONTH) != calendar2.get(Calendar.MONTH)
                || calendar1.get(calendar1.DAY_OF_MONTH) - calendar2.get(Calendar.DAY_OF_MONTH) > 2) {
            timeStr = getMiniTime(time);
        } else if (calendar1.get(calendar1.DAY_OF_MONTH) - calendar2.get(Calendar.DAY_OF_MONTH) == 2) {
            timeStr = "前天 " + getFormatTime(time, "HH点mm分ss秒");
        } else if (calendar1.get(calendar1.DAY_OF_MONTH) - calendar2.get(Calendar.DAY_OF_MONTH) == 1) {
            timeStr = "昨天 " + getFormatTime(time, "HH点mm分ss秒");
        } else {
            timeStr = "今天 " + getFormatTime(time, "HH点mm分ss秒");
        }
        return timeStr;
    }

    public static String getEasyWeekTime(long time) {
        if (time <= 0) {
            return null;
        }
        long currentSeconds = System.currentTimeMillis();
        long timeGap = currentSeconds - time;// 与现在时间差
        String timeStr = null;
        L.i(" " + (timeGap - (60 * 60 * 1000)));
        if (timeGap > 24 * 3 * 60 * 60 * 1000) {
            timeStr = getMiniTime(time);
        } else if (timeGap > 24 * 2 * 60 * 60 * 1000) {
            timeStr = "前天 " + getFormatTime(time, "HH点mm分ss秒");
        } else if (timeGap > 24 * 60 * 60 * 1000) {
            timeStr = "昨天 " + getFormatTime(time, "HH点mm分ss秒");
        } else {
            timeStr = "今天 " + getFormatTime(time, "HH点mm分ss秒");
        }
        return timeStr;
    }

    /**
     * 根据日期获得星期
     *
     * @return
     */
    public static String getWeekOfDate(long time) {
        String[] weekDaysName = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        String[] weekDaysCode = {"0", "1", "2", "3", "4", "5", "6"};
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return weekDaysCode[intWeek];
    }

    public static String getFormatTime(long time, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
        return sdf.format(new Date(time));
    }

    public static String getDay(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        return format.format(new Date(time));
    }

    public static String getMiniTime(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return format.format(new Date(time));
    }

    public static long getTimeFromMiniStr(String timeStr) {
        long time = 0;
        if (!StrUtil.isEmpty(timeStr)) {
            SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss", Locale.getDefault());
            try {
                Date date = format.parse(timeStr);
                time = date.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return time;
    }
}
